package net.hn.hnms.biz.dust.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import net.hn.hnms.biz.api.feign.model.DataStateVo;
import net.hn.hnms.biz.api.safety.support.domain.MineNowAlarmVo;
import net.hn.hnms.biz.common.jpa.utils.ResponsePage;
import net.hn.hnms.biz.dust.domain.DwdCoalSeamInfoEntity;
import net.hn.hnms.biz.dust.domain.DwdFcfzBaseSensorinfoEntity;
import net.hn.hnms.biz.dust.domain.DwdFcfzNowSensormonitorEntity;
import net.hn.hnms.biz.dust.repository.FcfzBaseRepository;
import net.hn.hnms.biz.dust.repository.FcfzNowRepository;
import net.hn.hnms.biz.dust.service.FcfzService;
import net.hn.hnms.biz.dust.vo.AlarmVo;
import net.hn.hnms.biz.dust.vo.ChartVo;
import net.hn.hnms.biz.dust.vo.FcfzData;
import net.hn.hnms.sys.common.core.domain.R;
import net.hn.hnms.sys.common.core.utils.StringUtils;
import net.hn.hnms.sys.common.satoken.config.AutoSetMineCode;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 智呈物联 pan'sx
 * 2022/10/19 16:59
 * 1.0 version
 */
@RestController
@RequestMapping("safetyMonitor")
@Api(value = "粉尘监控控制器", tags = {"粉尘监控"})
@RequiredArgsConstructor
public class FcfzController {
    private final FcfzNowRepository fcfzNowRepository;
    private final FcfzBaseRepository fcfzBaseRepository;
    private final FcfzService fcfzService;

    @GetMapping("nowData")
    @ApiOperation("粉尘实时数据")
    @AutoSetMineCode
    public R<List<DwdFcfzNowSensormonitorEntity>> nowData(String mineCode) {
        if(StringUtils.isNotBlank(mineCode)){
            return R.ok(fcfzNowRepository.findByMineCodeAndPointNameIsLike(mineCode,"%粉尘"));
        }
        return R.ok(fcfzNowRepository.findByPointNameIsLike("%粉尘"));
    }

    @GetMapping("deviceData")
    @ApiOperation(value = "粉尘治理设备", notes = "1是关 98是开")
    @AutoSetMineCode
    public R<List<DwdFcfzNowSensormonitorEntity>> deviceData(String mineCode) {
        List<DwdFcfzNowSensormonitorEntity> byPointNameIsLike;
        if(StringUtils.isNotBlank(mineCode)){
            byPointNameIsLike=fcfzNowRepository.findByMineCodeAndPointNameIsLike(mineCode,"%阀门");
        }else {
            byPointNameIsLike = fcfzNowRepository.findByPointNameIsLike("%阀门");
        }
        byPointNameIsLike.forEach(s -> {
            s.setDeviceType("洒水阀门");
        });
        return R.ok(byPointNameIsLike);
    }

    @GetMapping("alarm")
    @ApiOperation("报警数量")
    @AutoSetMineCode
    public R<AlarmVo> alarm(String mineCode) {
        /// 0 无实时数据

        /// 1 本地下限断电

        /// 2 本地上限断电

        /// 3 异地下限断电

        /// 4 异地上限断电

        /// 5 手动标校

        /// 6 自动标校

        /// 7 超量程

        /// 8 分站故障

        /// 9 不巡检

        /// 10 暂停

        /// 11 传感器故障

        /// 12 系统奔溃后，通信驱动启动时，默认先读取数据，在正确存入数据库前，先拷贝一份，测点S值改为256，即“100000000”，并时间改成软件系统奔溃的时间点（服务引擎实时将运行时间写入文件保存）存入数据库。此操作是供报表查询分析使用。

        /// 13 断线续传数据

        /// 14 断电异常

        /// 15 巡检周期内数据

        /// 16 强制存储数据

        /// 17 断线续传数据结束

        /// 18 终止读断线续传数据

        /// 19 传感器断线

        /// 20 手动维护放电

        /// 21 自动维护放电

        /// 22 网关故障

        /// 23 预热

        /// 24 检修

        /// 25 通道故障

        /// 26 断电故障异常

        /// 27 放炮

        /// 28 馈电异常

        /// 29 收发器故障
        AlarmVo vo = new AlarmVo();
        List<String> codes = new ArrayList<>();
        codes.add("8");
        codes.add("11");
        codes.add("22");
        codes.add("25");
        List<String> codess = new ArrayList<>();
        codess.add("5");
        codess.add("6");
        if(StringUtils.isNotBlank(mineCode)){
            vo.setFault(fcfzNowRepository.countByMineCodeAndPointStatusCodeIn(mineCode,codes));
            vo.setCalibration(fcfzNowRepository.countByMineCodeAndPointStatusCodeIn(mineCode,codess));
            vo.setSpray(fcfzNowRepository.countByMineCodeAndPointNameIsLikeAndPointValueGreaterThan(mineCode,"%阀门", new BigDecimal("0")));
        }else {
            vo.setFault(fcfzNowRepository.countByPointStatusCodeIn(codes));
            vo.setCalibration(fcfzNowRepository.countByPointStatusCodeIn(codess));
            vo.setSpray(fcfzNowRepository.countByPointNameIsLikeAndPointValueGreaterThan("%阀门", new BigDecimal("0")));
        }
        return R.ok(vo);
    }

    @GetMapping("alarmTrend")
    @ApiOperation("报警趋势")
    @AutoSetMineCode
    public R<ChartVo> alarmTrend(@ApiParam("周或月") @RequestParam(defaultValue = "周") String type, String mineCode) {
        return R.ok(fcfzService.alarmTrend(type,mineCode));
    }

    @GetMapping("concentrationTrend")
    @ApiOperation("粉尘趋势")
    @AutoSetMineCode
    public R<ChartVo> monitorTrend(@ApiParam("周或月") @RequestParam(defaultValue = "天") String type,
                                   @ApiParam(value = "测点编码", required = true) String pointCode,String mineCode) {
        return R.ok(fcfzService.monitorTrend(type, pointCode,mineCode));
    }

    @GetMapping("pageList")
    @ApiOperation("列表查询")
    @AutoSetMineCode
    public R<ResponsePage<FcfzData>> pageList(@RequestParam(defaultValue = "0") int pageNum, @RequestParam(defaultValue = "20") int pageSize,
                                              String beginTime, String endTime, String pointCode, String mineCode) {
        return R.ok(fcfzService.pageList(pageNum, pageSize, beginTime, endTime, pointCode,mineCode));
    }

    @GetMapping("baseSensorinfo")
    @ApiOperation("测点信息表")
    @AutoSetMineCode
    public R<List<DwdFcfzBaseSensorinfoEntity>> selectParam(String mineCode) {
        if(StringUtils.isNotBlank(mineCode)) {
            return R.ok(fcfzBaseRepository.findByMineCode(mineCode));
        }else{
            return R.ok(fcfzBaseRepository.findAll());
        }
    }
    @GetMapping("alarmByMine")
    @ApiOperation("各煤矿报警数")
    public List<MineNowAlarmVo> alarmByMine(){
        return fcfzNowRepository.alarmByMine();
    }

    @GetMapping("maxTime")
    @ApiOperation("数据最新时间")
    public R<List<DataStateVo>> getMaxTime(String mineCode, String companyCode){
        return R.ok(fcfzService.getMaxTime( mineCode,companyCode));
    }

    @GetMapping("/coalSeamInfo")
    @ApiOperation("煤尘特性分析")
    @AutoSetMineCode
    public R<DwdCoalSeamInfoEntity> coalSeamInfo(String mineCode) {
        return R.ok(fcfzService.coalSeamInfo(mineCode));
    }

}
